package rrrmai.activity;

import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

import com.xgggh.rrrmai.R;
import java.util.concurrent.TimeUnit;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import rrrmai.apps.ApplicationCrashHandler;
import xgggh.activity.BaseActivity;
import xgggh.manage.AuthManager;

public class StartUpActivity extends BaseActivity {
    // https://blog.csdn.net/a553181867/article/details/54799391
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ApplicationCrashHandler.installHandler();
       /* ItemTouchHelper.SimpleCallback swipeHandler = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
            Drawable background, xMark;
            int xMarkMargin;
            boolean initialized;

            private void initialize() {
                background = new ColorDrawable(Color.RED);
                xMark = ContextCompat.getDrawable(NoteListActivity.this, R.drawable.ic_clear_24dp);
                xMark.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
                xMarkMargin = (int) NoteListActivity.this.getResources().getDimension(R.dimen.ic_clear_margin);
                initialized = true;
            }

            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                return false;
            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
                final NoteViewHolder noteHolder = (NoteViewHolder) viewHolder;
                ((NotesAdapter) notesList.getAdapter()).remove(noteHolder);
            }

            @Override
            public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
                // If the item has already been swiped away, ignore
                if (viewHolder.getAdapterPosition() == -1) return;

                // If not initialized, then do so
                if (!initialized) initialize();

                int vr = viewHolder.itemView.getRight();
                int vt = viewHolder.itemView.getTop();
                int vb = viewHolder.itemView.getBottom();
                int vh = vb - vt;
                int iw = xMark.getIntrinsicWidth();
                int ih = xMark.getIntrinsicWidth();

                background.setBounds(vr + (int)dX, vt, vr, vb);
                background.draw(c);

                int xMarkLeft = vr - xMarkMargin - iw;
                int xMarkRight = vr - xMarkMargin;
                int xMarkTop = vt + (vh - ih)/2;
                int xMarkBottom = xMarkTop + ih;
                xMark.setBounds(xMarkLeft, xMarkTop, xMarkRight, xMarkBottom);
                xMark.draw(c);

                super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
            }
        };
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(swipeHandler);
        itemTouchHelper.attachToRecyclerView(notesList);

        // Kick off the data loader for the RecyclerView
        getLoaderManager().initLoader(NOTES_LOADER, null, this);
    }*/

        setContentView(R.layout.activity_start_up);
        if (!isTaskRoot()){
            finish();
        }

        AuthManager.init(getmRxSharedPreferences(),AuthManager.Token);

        Observable.timer(1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindToLifecycle())
                .subscribe(aLong -> startPage());
    }

    private void  startPage(){
        boolean _auth =AuthManager.isAuth();
         if (!_auth){
             startActivity(new Intent(this,LoginActivity.class));
         }else{
             startActivity(new Intent(this,MainActivity.class));
         }
         finish();
    }

}
